//
//  XCSearchController.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCSearchController.h"
#import "HYCellForSearch.h"
#import "XCSingerController.h"
#import "NetworkingHandlerOfBlock.h"
#import "HYModelForSearch.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface XCSearchController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableViewForSearch;
@property (nonatomic, strong) UIView *viewForHeader;
@property (nonatomic, strong) NSMutableArray *arrForSearch;

@end

@implementation XCSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arrForSearch = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 6)];
    
    self.viewForHeader.backgroundColor = [UIColor grayColor];
    
    
    
    [self.view addSubview:self.viewForHeader];

    [self createTableView];
    
    [self handleData];
        
}

- (void)handleData {
    
    NSLog(@"%@", self.url);
    
    [NetworkingHandler networkingWithUrl:self.url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            
            HYModelForSearch *model = [[HYModelForSearch alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrForSearch addObject:model];
            
        }
        
        
        [self.tableViewForSearch reloadData];
        
//        [self.tableViewForSearch.mj_footer endRefreshing];
        
    }];
    
}

- (void)createTableView {
    
    self.tableViewForSearch = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT / 6, WIDTH, HEIGHT - 49 - HEIGHT / 6) style:UITableViewStylePlain];
        
    [self.view addSubview:self.tableViewForSearch];
    
    self.tableViewForSearch.dataSource = self;
    self.tableViewForSearch.delegate = self;

    [self.tableViewForSearch registerClass:[HYCellForSearch class] forCellReuseIdentifier:@"searchMusic"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return self.arrForSearch.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HYCellForSearch *cell = [tableView dequeueReusableCellWithIdentifier:@"searchMusic"];
    
    HYModelForSearch *model = [self.arrForSearch objectAtIndex:indexPath.item];
    
    [cell passMoldel:model :indexPath.row];
    
    return cell;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
